<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

;

use GuzzleHttp\Promise;

$GLOBALS['appID'] = "0988E21B01B60906678E6C169A2699AD";
$GLOBALS['appKey'] = '1088957707187519101';
$GLOBALS['url'] = 'https://area42-win.pospal.cn:443/';

function get_post($url, $data, $headers)
{
    $client = new Client();
    try {
        $response = $client->post($GLOBALS['url'] . $url, [
            'headers' => $headers,
            'json' => $data
        ]);
        // 输出请求结果
        return $response->getBody();
    } catch (RequestException $e) {
        // 输出请求异常
        return $e->getMessage();
    }
}

$phone = $_POST['phone'];
if (empty($phone)) {
    echo '{"code":-1,"msg":"手机号不能为空"}';
    exit;
}
$body = [
    "appId" => $GLOBALS['appID'],
    "customerNum" => $phone
];
$headers = [
    'User-Agent' => 'openApi',
    'Content-Type' => 'application/json',
    'data-signature' => strtoupper(md5($GLOBALS['appKey'] . json_encode($body)))
];
$number = json_decode(get_post('pospal-api2/openapi/v1/customerOpenApi/generatePayToken', $body, $headers));
if ($number->status == 'success' && !empty($number->data)) {
    echo json_encode([
        'code' => 1,
        'data' => $number->data
    ]);
} else {
    echo json_encode([
        'code' => 0,
        'data' => $number->messages
    ]);
}
